<?php

require_once 'PHPUnit/Framework.php';

require dirname(__FILE__).'/../../../KenDon.php';

//require dirname(__FILE__).'/../../../KenDon/classes/KD_QTree.php';
KenDon::load('QTree');

//require dirname(__FILE__).'/../../../KenDon/Adapter/KD_Adapter_DB_PDO.php';
KenDon::load('DB_PDO', 'adapter');

class KD_QTreeTest extends PHPUnit_Framework_TestCase{

	protected $DBWrap ;

	protected $DBInfo ;

	protected $kd_db_pdo ;

	protected $tree ;

	protected $testData ;

	public function __construct(){
		$this->DBWrap = 'KD_Adapter_DB_PDO';
		//$this->DBInfo = array( 'dbms'=>'sqlite2', 'dbName'=> dirname(__FILE__) . '/../../kendon.sdb' );
		$this->DBInfo = array('dbms'=>'mysql', 'dbName'=>'kendon');
		$this->kd_db_pdo = new KD_Adapter_DB_PDO($this->DBInfo);  
	}

	public function setUp(){

		//删除数据表
		//
		/** < sqlite */
		//$sql_drop_table = 'DROP TABLE qtree;';
		/**< mysql */
		$sql_drop_table = 'DROP TABLE IF EXISTS `qtree`;';


		$this->kd_db_pdo->execute($sql_drop_table);

		//通过SQL生成数据库内容

		/**< sqlite */
		/*
    $sql_create_table = '
			CREATE TABLE qtree (
					id INTEGER  NOT NULL PRIMARY KEY,
					title VARCHAR(45)  NOT NULL,
					pre INTEGER NOT NULL,
					succ INTEGER NOT NULL,
					parent_id INTEGER);';
    */

		/**< mysql */
    //*
		$sql_create_table = '
			CREATE TABLE `qtree` (
					`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
					`title` varchar(45) NOT NULL,
					`pre` int(11) NOT NULL,
					`succ` int(11) NOT NULL,
					`parent_id` int(11) DEFAULT NULL,
					PRIMARY KEY (`id`)
					) ENGINE=InnoDB DEFAULT CHARSET=utf8;';
     //*/

		$this->kd_db_pdo->execute($sql_create_table);

		$sql_insert_data = array(
			 " insert into qtree (title,pre,succ,parent_id)  values('_virtual_root_',1,28,null);",       
			 " insert into qtree (title,pre,succ,parent_id)  values('s',2,27,1);",
			 " insert into qtree (title,pre,succ,parent_id)  values('KenDon',3,10,2);",
			 " insert into qtree (title,pre,succ,parent_id)  values('doc',4,5,3);",
			 " insert into qtree (title,pre,succ,parent_id)  values('test',6,7,3);",
			 " insert into qtree (title,pre,succ,parent_id)  values('lib',8,9,3);",
			 " insert into qtree (title,pre,succ,parent_id)  values('leo',11,18,2);",
			 " insert into qtree (title,pre,succ,parent_id)  values('kendon.leo',12,13,7);",
			 " insert into qtree (title,pre,succ,parent_id)  values('rencai.leo',14,15,7);",
			 " insert into qtree (title,pre,succ,parent_id)  values('myleo.leo',16,17,7);",
			 " insert into qtree (title,pre,succ,parent_id)  values('rencai5',19,26,2);",
			 " insert into qtree (title,pre,succ,parent_id)  values('app',20,21,11);",
			 " insert into qtree (title,pre,succ,parent_id)  values('core',22,23,11);",
			 " insert into qtree (title,pre,succ,parent_id)  values('docs',24,25,11);",
		);

		foreach($sql_insert_data as $item){
			$this->kd_db_pdo->execute($item);
		}


		$setting = array(
			'DBWrap'  =>  $this->DBWrap,
			'DBInfo'  =>  $this->DBInfo,
			'table'   =>  'qtree',
		);

		$this->tree = new KD_QTree($setting);

		$this->testData = array(
				array( 'id'=>1,  'pre'=>1,  'succ'=>28, 'parent_id'=>null, 'title'=>'_virtual_root_',),
				array( 'id'=>2,  'pre'=>2,  'succ'=>27, 'parent_id'=>1,    'title'=>'s',),
				array( 'id'=>3,  'pre'=>3,  'succ'=>10, 'parent_id'=>2,    'title'=>'KenDon',),
				array( 'id'=>4,  'pre'=>4,  'succ'=>5,  'parent_id'=>3,    'title'=>'doc',),
				array( 'id'=>5,  'pre'=>6,  'succ'=>7,  'parent_id'=>3,    'title'=>'test',),
				array( 'id'=>6,  'pre'=>8,  'succ'=>9,  'parent_id'=>3,    'title'=>'lib',),
				array( 'id'=>7,  'pre'=>11, 'succ'=>18, 'parent_id'=>2,    'title'=>'leo',),
				array( 'id'=>8,  'pre'=>12, 'succ'=>13, 'parent_id'=>7,    'title'=>'kendon.leo',),
				array( 'id'=>9,  'pre'=>14, 'succ'=>15, 'parent_id'=>7,    'title'=>'rencai.leo',),
				array( 'id'=>10, 'pre'=>16, 'succ'=>17, 'parent_id'=>7,    'title'=>'myleo.leo',),
				array( 'id'=>11, 'pre'=>19, 'succ'=>26, 'parent_id'=>2,    'title'=>'rencai5',),
				array( 'id'=>12, 'pre'=>20, 'succ'=>21, 'parent_id'=>11,   'title'=>'app',),
				array( 'id'=>13, 'pre'=>22, 'succ'=>23, 'parent_id'=>11,   'title'=>'core',),
				array( 'id'=>14, 'pre'=>24, 'succ'=>25, 'parent_id'=>11,   'title'=>'docs',),
			); 
	}

	public function dearDown(){
	}

	public function testModelExists(){
		$this->assertEquals(true, is_a($this->kd_db_pdo, 'KD_Adapter_DB_PDO'));
	}

	public function testTreeExists(){
		$this->assertEquals(true, is_a( $this->tree, 'KD_QTree') ); 
	}

	public function test_isNode(){
		foreach($this->testData as $item){
			$this->assertEquals(true, $this->tree->isNode($item) == true);
			$this->assertEquals(true, $this->tree->isNode( array() ) == false);
		}  
	}

	public function test_getNode(){
		/*    
		$count = count($this->testData);

		do{

			$node =  $this->tree->getNode($count);

			$this->assertEquals(true, $node['id'] == $count );
		}while($count--);
		*/

		$node = $this->tree->getNode(1);
		$this->assertEquals(count($node), 5);
		$this->assertEquals($node['title'] , '_virtual_root_');  
	}

	public function test_getRootNode(){

		$rootNode = $this->tree->getRootNode();

		$this->assertEquals(true, is_array($rootNode));

		$this->assertEquals(true, count($rootNode) >0 );

		$this->assertEquals($rootNode['id'], '1');
		$this->assertEquals(true, $rootNode['title'] === '_virtual_root_');
		$this->assertEquals(true, $rootNode['pre'] === '1');
		$this->assertEquals(true, $rootNode['succ'] === '28');
		$this->assertEquals(true, $rootNode['parentid'] === null);

		$this->assertEquals(true, $rootNode['id'] == $this->testData[0]['id']);
		$this->assertEquals(true, $rootNode['title'] === $this->testData[0]['title']);
		$this->assertEquals(true, $rootNode['pre'] == $this->testData[0]['pre']);
		$this->assertEquals(true, $rootNode['succ'] == $this->testData[0]['succ']);
		$this->assertEquals(true, $rootNode['parentid'] == $this->testData[0]['parentid']);

		$this->assertEquals(true, count($rootNode) === count($this->testData[0]) );

		$this->assertEquals(true, $rootNode == $this->testData[0]);
	}

	public function test_getICnodes(){

		$test = array(
			array(
	 'nodes'     =>  $this->tree->getICNodes( $this->tree->getNode(1) ),
	 'testdata'  =>  array($this->testData[1]),
 ),

 array(
	 'nodes'     =>  $this->tree->getICNodes( $this->tree->getNode(7) ),
	 'testdata'  =>  array( $this->testData[7], $this->testData[8], $this->testData[9] ),     
 ),

 array(
	 'nodes'     =>  $this->tree->getICNodes( $this->tree->getNode(13) ),
	 'testdata'  =>  false,    
 )
		);

		$this->assertEquals(true, $test[0]['nodes'] == $test[0]['testdata']);  
		$this->assertEquals( $test[1]['nodes'] , $test[1]['testdata']);  
		$this->assertEquals(true, $test[2]['nodes'] == $test[2]['testdata']);  

	}

	public function test_getAllNodes(){
		$nodes = $this->tree->getAllNodes();
		$this->assertEquals(true, $nodes == $this->testData);  
	}

	public function test_getParentNode(){
		$test = array(
				array('node'=> $this->tree->getParentNode($this->tree->getNode(1)), 'testdata'=>false,),

				array('node'=> $this->tree->getParentNode($this->tree->getNode(2)), 'testdata'=>$this->testData[0],),

				array('node'=> $this->tree->getParentNode($this->tree->getNode(7)), 'testdata'=>$this->testData[1],),

				array('node'=> $this->tree->getParentNode($this->tree->getNode(6)), 'testdata'=>$this->testData[2],),
		);

		//$this->assertEquals($test[0]['node'], '');
		$this->assertEquals(true, $test[0]['node'] == $test[0]['testdata']);
		$this->assertEquals(true, $test[1]['node'] == $test[1]['testdata']);
		$this->assertEquals(true, $test[2]['node'] == $test[2]['testdata']);
		$this->assertEquals(true, $test[3]['node'] == $test[3]['testdata']);
	}

	public function test_getChildCount(){
		$test = array(
				array('count' => $this->tree->getChildCount($this->tree->getNode(1)), 'testdata' => 13,),
				array('count' => $this->tree->getChildCount($this->tree->getNode(2)), 'testdata' => 12,),
				array('count' => $this->tree->getChildCount($this->tree->getNode(3)),'testdata'  =>  3,),
				array('count' => $this->tree->getChildCount($this->tree->getNode(4)),'testdata'  =>  0,),
		);

	 $this->assertEquals(true, $test[0]['count'] == $test[0]['testdata'] ); 
	 $this->assertEquals(true, $test[1]['count'] == $test[1]['testdata'] );
	 $this->assertEquals(true, $test[2]['count'] == $test[2]['testdata'] );
	 $this->assertEquals(true, $test[3]['count'] == $test[3]['testdata'] );

	}

	public function test_getNodePath(){
		$tree = $this->tree;
		$testData = $this->testData;

		$test = array(
				array('ancestorNode'=> $tree->getNodePath($tree->getNode(1)), 'testdata'=> false),
				array('ancestorNode'=> $tree->getNodePath($tree->getNode(2)), 'testdata'=> array($testData[0])),
				array('ancestorNode'=> $tree->getNodePath($tree->getNode(3)), 'testdata'=> array($testData[0], $testData[1])),
				array('ancestorNode'=> $tree->getNodePath($tree->getNode(4)), 'testdata'=> array($testData[0], $testData[1], $testData[2])),
		); 

		$this->assertEquals(true, $test[0]['ancestorNode'] == $test[0]['testdata']);
		$this->assertEquals(true, $test[1]['ancestorNode'] == $test[1]['testdata']);
		//$this->assertEquals(true, $test[2]['ancestorNode'] == $test[2]['testdata']);
	}

	public function test_getSubNodes(){
		$tree = $this->tree;

		$testData = $this->testData;

		$testData1 = $testData;

		$testData2 = $testData;

		array_shift($testData2); 

		$testData3 = array($testData[2], $testData[3], $testData[4], $testData[5]);

		$testData4 = array($testData[3]);

		$test = array(
				array('ancestorNode'=> $tree->getSubNodes( $tree->getNode(1) ), 'testdata'=> $testData1 ),
				array('ancestorNode'=> $tree->getSubNodes( $tree->getNode(2) ), 'testdata'=> $testData2 ),
				array('ancestorNode'=> $tree->getSubNodes( $tree->getNode(3) ), 'testdata'=> $testData3 ),
				array('ancestorNode'=> $tree->getSubNodes( $tree->getNode(4) ), 'testdata'=> $testData4 ),
		); 

		$this->assertEquals(true, $test[0]['ancestorNode'] == $test[0]['testdata']);
		$this->assertEquals(true, $test[1]['ancestorNode'] == $test[1]['testdata']);
		$this->assertEquals(true, $test[2]['ancestorNode'] == $test[2]['testdata']);
		$this->assertEquals(true, $test[3]['ancestorNode'] == $test[3]['testdata']);

	}

	public function test_getFCNode(){

		$tree = $this->tree;

		$testData = $this->testData;

		$test = array(
				1 => array('ancestorNode'=> $tree->getFCNode( $tree->getNode(1) ), 'testdata'=> $testData[1] ),
				2 => array('ancestorNode'=> $tree->getFCNode( $tree->getNode(2) ), 'testdata'=> $testData[2] ),
				3 => array('ancestorNode'=> $tree->getFCNode( $tree->getNode(3) ), 'testdata'=> $testData[3] ),
				4 => array('ancestorNode'=> $tree->getFCNode( $tree->getNode(4) ), 'testdata'=> false ),
		); 

		$this->assertEquals(true, $test[1]['ancestorNode'] == $test[1]['testdata']);
		$this->assertEquals(true, $test[2]['ancestorNode'] == $test[2]['testdata']);
		$this->assertEquals(true, $test[3]['ancestorNode'] == $test[3]['testdata']);
		$this->assertEquals(true, $test[3]['ancestorNode'] == $test[3]['testdata']);
	}

	public function test_getSLNodes(){
		$tree = $this->tree;

		$testData = $this->testData;

		$test = array(
				array($tree->getSLNodes($tree->getNode(1)) , false ),
				array($tree->getSLNodes($tree->getNode(2)) , false ),
				array($tree->getSLNodes($tree->getNode(3)) , array($testData[6], $testData[10]) ),
				array($tree->getSLNodes($tree->getNode(4)) , array($testData[4], $testData[5]) ),
		);

		$this->assertEquals(true, $test[0][0] == $test[0][1]);
		$this->assertEquals(true, $test[1][0] == $test[1][1]);
		$this->assertEquals($test[2][0] , null);
		$this->assertEquals($test[3][0] , null);

	}

	/** targetNode ['title'] = 'testNode'; 
	 *  refer ['rootNode, leftmost, middle, rightmost, leaf, notNode]
	 *  result['rootNode']['after'], result['rootNode']['befor'], result['rootNode']['below']
	 *  allNodes['rootNode']['after'], allNodes['rootNode']['before'], allNodes['rootNode']['below']
	 *  expect['rootNode']['after'], expect['rootNode']['befor'], expect['rootNode']['below']
	 */
	public function test_insetNode(){

		$targetNode = array('title'=>'testNode');

		$refer      = array();

		$result     = array();

		$allNodes   = array();

		$expect     = array();

		//1. rootNode
		$refer['rootNode']    = $this->tree->getRootNode();
		$result['rootNode']   = array();
		$allNodes['rootNode'] = array();
		$expect['rootNode']   = array();

		//1.1 position: after, expect null;

		$result['rootNode']['after'] = $this->tree->insertNode($targetNode, $refer['rootNode'], 'after');
		$this->assertEquals(null, $result['rootNode']['after']);
		$this->setUp();

		//1.2 position: before, expect null;
		$result['rootNode']['before'] = $this->tree->insertNode($targetNode, $refer['rootNode'], 'before');
		$this->assertEquals(null, $result['rootNode']['before']);
		$this->setUp();

		//1.3 position: below, expect true;
		$result['rootNode']['below'] = $this->tree->insertNode($targetNode, $refer['rootNode'], 'below');

		$allNodes['rootNode']['below'] = $this->tree->getAllNodes();

		$expect['rootNode']['below'] =  array(
				array( 'id'=>1,  'pre'=>1,  'succ'=>30, 'parent_id'=>null, 'title'=>'_virtual_root_',),
				array( 'id'=>2,  'pre'=>2,  'succ'=>27, 'parent_id'=>1,    'title'=>'s',),
				array( 'id'=>3,  'pre'=>3,  'succ'=>10, 'parent_id'=>2,    'title'=>'KenDon',),
				array( 'id'=>4,  'pre'=>4,  'succ'=>5,  'parent_id'=>3,    'title'=>'doc',),
				array( 'id'=>5,  'pre'=>6,  'succ'=>7,  'parent_id'=>3,    'title'=>'test',),
				array( 'id'=>6,  'pre'=>8,  'succ'=>9,  'parent_id'=>3,    'title'=>'lib',),
				array( 'id'=>7,  'pre'=>11, 'succ'=>18, 'parent_id'=>2,    'title'=>'leo',),
				array( 'id'=>8,  'pre'=>12, 'succ'=>13, 'parent_id'=>7,    'title'=>'kendon.leo',),
				array( 'id'=>9,  'pre'=>14, 'succ'=>15, 'parent_id'=>7,    'title'=>'rencai.leo',),
				array( 'id'=>10, 'pre'=>16, 'succ'=>17, 'parent_id'=>7,    'title'=>'myleo.leo',),
				array( 'id'=>11, 'pre'=>19, 'succ'=>26, 'parent_id'=>2,    'title'=>'rencai5',),
				array( 'id'=>12, 'pre'=>20, 'succ'=>21, 'parent_id'=>11,   'title'=>'app',),
				array( 'id'=>13, 'pre'=>22, 'succ'=>23, 'parent_id'=>11,   'title'=>'core',),
				array( 'id'=>14, 'pre'=>24, 'succ'=>25, 'parent_id'=>11,   'title'=>'docs',),
				array( 'id'=>15, 'pre'=>28, 'succ'=>29, 'parent_id'=>1,    'title'=>'testNode',),
				);

		$this->assertEquals(true, $result['rootNode']['below']);
		$this->assertEquals(true, $allNodes['rootNode']['below'] == $expect['rootNode']['below']);
		$this->setUp();

		//2. leftmostNode
		$refer['leftmostNode']    = $this->tree->getNode(3);
		$result['leftmostNode']   = array();
		$allNodes['leftmostNode'] = array();
		$expect['leftmostNode']   = array();

		//2.1 after
		$result['leftmostNode']['after'] = $this->tree->insertNode($targetNode, $refer['leftmostNode'], 'after');
		$allNodes['leftmostNode']['after'] = $this->tree->getAllNodes();
		$except['leftmostNode']['after'] = array(
			array('id'=>1,  'pre'=>1,   'succ'=>30, 'parent_id'=>null, 'title'=>'_virtual_root_'),
			array('id'=>2,  'pre'=>2,   'succ'=>29, 'parent_id'=>1,    'title'=>'s'),
			array('id'=>3,  'pre'=>3,   'succ'=>10, 'parent_id'=>2,    'title'=>'KenDon'),
			array('id'=>4,  'pre'=>4,   'succ'=>5,  'parent_id'=>3,    'title'=>'doc'),
			array('id'=>5,  'pre'=>6,   'succ'=>7,  'parent_id'=>3,    'title'=>'test'),
			array('id'=>6,  'pre'=>8,   'succ'=>9,  'parent_id'=>3,    'title'=>'lib'),
			array('id'=>15, 'pre'=>11,  'succ'=>12, 'parent_id'=>2,    'title'=>'testNode'),
			array('id'=>7,  'pre'=>13,  'succ'=>20, 'parent_id'=>2,    'title'=>'leo'),
			array('id'=>8,  'pre'=>14,  'succ'=>15, 'parent_id'=>7,    'title'=>'kendon.leo'),
			array('id'=>9,  'pre'=>16,  'succ'=>17, 'parent_id'=>7,    'title'=>'rencai.leo'),
			array('id'=>10, 'pre'=>18,  'succ'=>19, 'parent_id'=>7,    'title'=>'myleo.leo'),
			array('id'=>11, 'pre'=>21,  'succ'=>28, 'parent_id'=>2,    'title'=>'rencai5'),
			array('id'=>12, 'pre'=>22,  'succ'=>23, 'parent_id'=>11,   'title'=>'app'),
			array('id'=>13, 'pre'=>24,  'succ'=>25, 'parent_id'=>11,   'title'=>'core'),
			array('id'=>14, 'pre'=>26,  'succ'=>27, 'parent_id'=>11,   'title'=>'docs'),
		);
		$this->assertEquals(true, $result['leftmostNode']['after']);
		$this->setUp();

		//2.2 before
		$result['leftmostNode']['before'] = $this->tree->insertNode($targetNode, $refer['leftmostNode'], 'before');
		$allNodes['leftmostNode']['before'] = $this->tree->getAllNodes();
		$except['leftmostNode']['before'] = array(
			array('id'=>1,  'pre'=>1,   'succ'=>30, 'parent_id'=>null, 'title'=>'_virtual_root_'),
			array('id'=>2,  'pre'=>2,   'succ'=>29, 'parent_id'=>1,    'title'=>'s'),
			array('id'=>15, 'pre'=>3,   'succ'=>4,  'parent_id'=>2,    'title'=>'testNode'),
			array('id'=>3,  'pre'=>5,   'succ'=>12, 'parent_id'=>2,    'title'=>'KenDon'),
			array('id'=>4,  'pre'=>6,   'succ'=>7,  'parent_id'=>3,    'title'=>'doc'),
			array('id'=>5,  'pre'=>8,   'succ'=>9,  'parent_id'=>3,    'title'=>'test'),
			array('id'=>6,  'pre'=>10,  'succ'=>11, 'parent_id'=>3,    'title'=>'lib'),
			array('id'=>7,  'pre'=>13,  'succ'=>20, 'parent_id'=>2,    'title'=>'leo'),
			array('id'=>8,  'pre'=>14,  'succ'=>15, 'parent_id'=>7,    'title'=>'kendon.leo'),
			array('id'=>9,  'pre'=>16,  'succ'=>17, 'parent_id'=>7,    'title'=>'rencai.leo'),
			array('id'=>10, 'pre'=>18,  'succ'=>19, 'parent_id'=>7,    'title'=>'myleo.leo'),
			array('id'=>11, 'pre'=>21,  'succ'=>28, 'parent_id'=>2,    'title'=>'rencai5'),
			array('id'=>12, 'pre'=>22,  'succ'=>23, 'parent_id'=>11,   'title'=>'app'),
			array('id'=>13, 'pre'=>24,  'succ'=>25, 'parent_id'=>11,   'title'=>'core'),
			array('id'=>14, 'pre'=>26,  'succ'=>27, 'parent_id'=>11,   'title'=>'docs'),
		);
		$this->assertEquals(true, $result['leftmostNode']['before']);
		$this->assertEquals($allNodes['leftmostNode']['before'] , $except['leftmostNode']['before']);
		$this->setUp();

		//2.3 below
		$result['leftmostNode']['below'] = $this->tree->insertNode($targetNode, $refer['leftmostNode'], 'below');
		$allNodes['leftmostNode']['below'] = $this->tree->getAllNodes();
		$except['leftmostNode']['below'] = array(
				array( 'id'=>1,  'pre'=>1,  'succ'=>30, 'parent_id'=>null, 'title'=>'_virtual_root_',),
				array( 'id'=>2,  'pre'=>2,  'succ'=>29, 'parent_id'=>1,    'title'=>'s',),
				array( 'id'=>3,  'pre'=>3,  'succ'=>12, 'parent_id'=>2,    'title'=>'KenDon',),
				array( 'id'=>4,  'pre'=>4,  'succ'=>5,  'parent_id'=>3,    'title'=>'doc',),
				array( 'id'=>5,  'pre'=>6,  'succ'=>7,  'parent_id'=>3,    'title'=>'test',),
				array( 'id'=>6,  'pre'=>8,  'succ'=>9,  'parent_id'=>3,    'title'=>'lib',),
				array( 'id'=>15, 'pre'=>10, 'succ'=>11, 'parent_id'=>3,    'title'=>'testNode',),
				array( 'id'=>7,  'pre'=>13, 'succ'=>20, 'parent_id'=>2,    'title'=>'leo'),
				array( 'id'=>8,  'pre'=>14, 'succ'=>15, 'parent_id'=>7,    'title'=>'kendon.leo'),
				array( 'id'=>9,  'pre'=>16, 'succ'=>17, 'parent_id'=>7,    'title'=>'rencai.leo'),
				array( 'id'=>10, 'pre'=>18, 'succ'=>19, 'parent_id'=>7,    'title'=>'myleo.leo'),
				array( 'id'=>11, 'pre'=>21, 'succ'=>28, 'parent_id'=>2,    'title'=>'rencai5'),
				array( 'id'=>12, 'pre'=>22, 'succ'=>23, 'parent_id'=>11,   'title'=>'app'),
				array( 'id'=>13, 'pre'=>24, 'succ'=>25, 'parent_id'=>11,   'title'=>'core'),
				array( 'id'=>14, 'pre'=>26, 'succ'=>27, 'parent_id'=>11,   'title'=>'docs'),
			 );
		$this->assertEquals(true, $result['leftmostNode']['below']);
		$this->assertEquals(true, $allNodes['leftmostNode']['below'] == $except['leftmostNode']['below']);
		$this->setUp();

		//3.middleNode
		//4.rightmostNode
		//5.leftmostLeaf
		//6.middleLeaf
		//7.rightmostLeft
		//8.!isNode:
	}

	/** targetNode
	 *  result
	 *  allNodes
	 *  expect
	 */
	public function test_deleteNode(){

		$targetNode = array();
		$result     = array();
		$allNodes   = array();
		$except     = array();

		//1. root
		$targetNode['root'] = $this->tree->getRootNode();
		$result['root'] = $this->tree->deleteNode($targetNode['root']);

		$allNodes['root'] = $this->tree->getAllNodes();

		$except['root'] = array();

		$this->assertEquals(true, $result['root']); 
		$this->assertEquals(true, $allNodes['root'] == $except['root']);

		$this->setUp();

		//2.leftmostNode
		$targetNode['leftmostNode'] = $this->tree->getNode(3);
		$result['leftmostNode'] = $this->tree->deleteNode($targetNode['leftmostNode']);
    $this->assertEquals(true,$result['leftmostNode']);//2010.03.05 add @author jun<kenlixin@gmail.com>
		$allNodes['leftmostNode'] = $this->tree->getAllNodes();
		$except['leftmostNode'] = array(
				array( 'id'=>1,  'pre'=>1,  'succ'=>20, 'parent_id'=>null, 'title'=>'_virtual_root_',),
				array( 'id'=>2,  'pre'=>2,  'succ'=>19, 'parent_id'=>1,    'title'=>'s',),
				array( 'id'=>7,  'pre'=>3,  'succ'=>10, 'parent_id'=>2,    'title'=>'leo'),
				array( 'id'=>8,  'pre'=>4,  'succ'=>5,  'parent_id'=>7,    'title'=>'kendon.leo'),
				array( 'id'=>9,  'pre'=>6,  'succ'=>7,  'parent_id'=>7,    'title'=>'rencai.leo'),
				array( 'id'=>10, 'pre'=>8,  'succ'=>9,  'parent_id'=>7,    'title'=>'myleo.leo'),
				array( 'id'=>11, 'pre'=>11, 'succ'=>18, 'parent_id'=>2,    'title'=>'rencai5'),
				array( 'id'=>12, 'pre'=>12, 'succ'=>13, 'parent_id'=>11,   'title'=>'app'),
				array( 'id'=>13, 'pre'=>14, 'succ'=>15, 'parent_id'=>11,   'title'=>'core'),
				array( 'id'=>14, 'pre'=>16, 'succ'=>17, 'parent_id'=>11,   'title'=>'docs'),

		);

		$this->assertEquals(true, $result['leftmostNode']); 
		$this->assertEquals(true, $allNodes['leftmostNode'] == $except['leftmostNode']);

	}
  
	public function test_init(){
		$this->tree->init();
		$root = $this->tree->getRootNode();
		$this->assertEquals($root['title'], '_virtual_root_');  
	}
}
